package com.hundsun.cprs.yyt.web.vo;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;

/**
 * 功能说明: Json模型，用于响应ajax操作<br>
 * 注意事项: <br>
 * 系统版本: v1.0<br>
 * 开发人员: <br>
 * 开发时间: <br>
 */
@SuppressWarnings("serial")
public class JsonModel implements Serializable {
	
	public static final int RESULD_CODE_SUCCESS = 0;//成功
	
	public static final int RESULD_CODE_FAIL = 400;//失败

	private int code = 0;// 默认操作结果
	private String msg = "操作成功!";// 默认提示信息
	private Object data = ""; //url用于ajax返回首页


	public JsonModel() {
		
	}

	public JsonModel(int code) {
		this.setCode(code);
		if (code!=0) {
			this.setMsg("操作失败!");
		}
	}


	public JsonModel(Exception be) {
		this.setException(be);
	}

	public JsonModel(Object message) {
		this.setMsg(message);
	}

	public JsonModel(int code, String message) {
		this.setCode(code);
		this.setMsg(message);
	}


	public JsonModel(int code, Object message) {
		this.setCode(code);
		this.setMsg(message);
	}
	
    public JsonModel(int code, Object message,Object data) {
        this.setCode(code);
        this.setMsg(message);
        setData(data);
    }

	public int getCode() {
		return this.code;
	}

	public JsonModel setCode(int code) {
		this.code = code;
		return this;
	}


	public JsonModel setMsg(Object msg) {
		if (msg instanceof String) {
			this.msg = (String) msg;
			if (StringUtils.isBlank(this.msg)) {
				this.msg = code==0 ? "操作成功！" : "操作失败！";
			}
		} else {
			this.msg = JSON.toJSONString(msg);
		}
		return this;
	}


	public JsonModel setException(Exception be) {
		this.setCode(-1);
		if (be != null) {
			return this.setMsg(be.getMessage());
		} else {
			throw new RuntimeException("Exception对象不能为空");
		}
	}

	public String getMsg() {
		return msg;
	}


	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		if(data instanceof String){
			this.data = data;
		}else{
			this.data = JSON.toJSONString(data);
		}
	}

	
}
